import { proxy } from "valtio"
import { get, post } from "../request"
import { io } from 'socket.io-client';
interface StatusList {
    time: string,
    status: string
}
interface OrderDetails {
    ProductName: string,
    standard: string
    property?: string,
    originalprice: number,
    realprice: number,
    count: number,
    Lunchboxfee: number,
    price: number
}
interface DataType {
    //订单来源
    origin: string;
    //门店
    outlet: string;
    //订单号
    OrderID: number;
    //桌号
    TableModel: number;
    //成交时间
    DealTime: string;
    //品牌
    brand: string;
    //金额
    price: number;
    //买家
    seller: string;
    //手机号
    phone: number;
    //虚拟手机号
    xuphone: number;
    //收货地址
    ShippingAddress: string;
    //订单备注
    remark: string;
    //城市
    city: string;
    //订单明细
    OrderDetails: Array<OrderDetails>;
    //配送费
    delivery: number;
    //状态
    status: string
    //会员配送费
    vip: number;
    //订单活动总额
    numsum: number;
    //总价
    sum: number;
    //订单状态
    OrderStatus: Array<StatusList>;
}
const socket = io('http://localhost:3000')
export const producestore = proxy({
    origin: "",
    starttime: "",
    endtime: "",
    status: [],
    outlet: "",
    OrderID: null,
    hell: "",
    data: [] as DataType[],
    Datalist: {
        //订单来源
        origin: "",
        //门店
        outlet: "",
        //订单号
        OrderID: null,
        //桌号
        TableModel: null,
        //成交时间
        DealTime: "",
        //品牌
        brand: "",
        //金额
        price: null,
        //买家
        seller: "",
        //手机号
        phone: null,
        //虚拟手机号
        xuphone: null,
        //收货地址
        ShippingAddress: "",
        //订单备注
        remark: "",
        //城市
        city: "",
        //订单明细
        OrderDetails: [],
        //配送费
        delivery: null,
        //状态
        status: "",
        //会员配送费
        vip: null,
        //订单活动总额
        numsum: null,
        //总价
        sum: null,
        //订单状态
        OrderStatus: [],
    },
    showlist: (item: any) => {
        producestore.Datalist = item
    },
    searchlist: (origin: string, time: Array<any>, outlet: string, status: Array<any>, OrderID: number | null) => {
        producestore.OrderID = OrderID
        producestore.status = status
        producestore.outlet = outlet
        producestore.starttime = time[0]
        producestore.endtime = time[1]
        producestore.origin = origin;
    },
    reactlist: () => {
        producestore.origin = "";
    },
    getList: () => {
        get("/order").then((res) => {
            console.log(res);
            
            producestore.data = res as DataType[];
        })
    },

    search: () => {
        post("/order/selectOrder", { origin: producestore.origin, startDate: producestore.starttime, endDate: producestore.endtime, outlet: producestore.outlet, status: producestore.status, OrderID: producestore.OrderID }).then((res) => {
            producestore.data = res.data as DataType[];
        })
    },
    sendUpdate: (data: string) => {
        socket.emit('message', data);
    },
    initSocket: () => {
        socket.on('message', (data: any) => {
            // 更新组件状态或 UI
            producestore.getList()
        });
        return () => {
            socket.off('message');
        };
    }
})
